import $service from '../../../service.js';
var util = require('../../../utils/util.js')
var hospital_id = '1';
var doctor_id;
var page = 1; //页数
var that;
var queryzt = '';
var startTime;
var endTime;
var app = getApp();
Page({
    data: {
        hospitalType: $service.hospitalType,
        userlist: [],
        loadingText: '',
        loadingType: true,
        startdate: '',
        enddate: '',
        starttime: '2000-01-01',
        endtime: '',
        isshow: false,
        prescription_type: '请选择',
        zt: '请选择',
        patient_phone: '',
        patient_name: '',
        isprescriptiontypeshow: false,
        isztshow: false,
        isSendShow: false,
        name: '',
        sendData: {},
        noticeContent: '',
        list: [{
            name: '全部',
            val: 0
        },{
            name:'家庭医生患者',
            val: 3
        }],
        topActive: 0
    },
    onLoad(e) {
        //加载本页面的tabBar样式
        app.editTabBar();
    },
    onShow() {
        page = 1
        that = this.data
        wx.hideHomeButton({})
        this.getMysubscriber();
    },
    onPullDownRefresh() {
        page = 1;
        this.setData({
            loadingText: '加载更多'
        })
        this.getMysubscriber();
        wx.stopPullDownRefresh()
    },
    onReachBottom: function () {
        if (that.loadingType == true) {
            page++
            this.getMysubscriber();
        } else {
            this.setData({
                loadingText: '没有更多了'
            })
        }
    },
    // 我的用户数据
    getMysubscriber() {
        let params = {
            "inquiryType": this.data.topActive,
            "current": page,
            "maxTime": that.enddate,
            "minTime": that.startdate,
            "name": "",
            "phone": "",
            "size": 10
        }
        app.http.getMysubscriber(params, true).then(res => {
            let list = res.data.list;
            if (page == 1) {
                that.loadingText = '';
                that.userlist = list;
            } else {
                if (list.length <= 0) {
                    that.loadingType = false;
                    that.loadingText = '没有更多了';
                } else {
                    for (var i = 0; i < list.length; i++) {
                        that.userlist.push(list[i]);
                        that.loadingText = '加载更多';
                    }
                }
            }
            this.setData({
                userlist: that.userlist
            })
        }).catch(err => {
            $service.showToast(err.msg);
        })
    },
    loading() {
        if (that.loadingType == true) {
            page++
            this.getMysubscriber();
        } else {
            this.setData({
                loadingText: '没有更多了'
            })
        }
    },
    //时间
    bookTime(e) {
        page = 1;
        let time = e.detail.time;
        let type = e.currentTarget.dataset.type;
        if (type === 'start') {
            if (that.enddate) {
                if ($service.timeFormat(time) > $service.timeFormat(that.enddate)) {
                    $service.showToast('结束时间不可小于开始时间');
                    return
                }
                that.startdate = time;
            } else {
                that.startdate = time;
            }
        } else {
            if (that.startdate) {
                if ($service.timeFormat(that.startdate) > $service.timeFormat(time)) {
                    $service.showToast('结束时间不可小于开始时间');
                    return
                }
            }
            that.enddate = time;
        }
        this.setData({
            startdate: that.startdate,
            enddate: that.enddate
        })
        this.getMysubscriber();
    },
    // 发送消息
    sendMes(e) {
        let index = e.currentTarget.dataset.index;
        let data = this.data.userlist[index]
        this.setData({
            isSendShow: true,
            name: data.patientName,
            sendData: data
        })
    },
    sendChange(e) {
        this.setData({
            noticeContent: e.detail.value
        })
    },
    send(e) {
        let num = e.currentTarget.dataset.num;
        if (num == 1){ 
            let params = {
                "noticeContent": this.data.noticeContent,
                "userId": this.data.sendData.userId
              }
            app.http.sendNews(params).then(res => {
                $service.showToast('发送成功')
                this.setData({
                    noticeContent: '',
                    isSendShow: false
                })
            }).catch(err => {
                $service.showToast(err.msg);
            })
        } else {
            this.setData({
                isSendShow: false
            })
        }
    },
    // 查看病历
    viewRecord(e) {
        let index = e.currentTarget.dataset.index;
        let data = this.data.userlist[index]
        let url = $service.isHis ? `/pages/doctor_side/my_users/liangjiang-inquiry-history-list/liangjiang-inquiry-history-list?patientId=${data.patientId}&healthCardNo=${data.healthCardNo}&patientName=${data.patientName}` : `/pages/doctor_side/index/index_history/index_history?idCard=${data.patientIdcard}&name=${data.patientName}&logId=${data.logId}`
        $service.openPage(url)
    },
    changeTab(e) {
        let {val} = e.currentTarget.dataset
        this.setData({
            topActive: val
        })
        this.getMysubscriber();
    },
    viewChat(e) {
        let {userId, userSource} = this.data.userlist[e.currentTarget.dataset.index]
        $service.openPage(`./chat_log/chat_log?userId=${userId}&userSource=${userSource}`)
    }
})